import axios from "axios"
import apiConfig from "@/utils/apiConfig"
import router from '../router'
import { ElMessage } from "element-plus";

const request = axios.create({
  baseURL: apiConfig.baseURL,
  timeout: 300000,
})

request.interceptors.request.use(
  (config) => {
    config.headers['Content-Type'] = 'application/json'
    config.headers['Authorization'] = `${localStorage.getItem('token')}` || ''
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

request.interceptors.response.use(
  (res) => {
    if (res.status == 200) {
      return res.data
    } else {
      Promise.reject(res)
    }
  },
  (err) => {
    ElMessage.error("前端报错");
    // router.push({
    //   path: '/login'
    // })
    return Promise.reject(err)
  }
);

export default request
